package com.lidlib.lib;

import java.io.IOException;
import java.io.InputStream;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetManager;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.util.ArrayList;

class DialogMessage {
	public String title;
	public String message;
	
	public DialogMessage(String title, String message){
		this.message = message;
		this.title = title;
	}
}


public class LidlibCommon 
{
	private static native void nativeSetPaths(String apkPath);
	
	private static Context sContext;
	private static String sPackageName;
	private static Handler handler;
	private final static int HANDLER_SHOW_DIALOG = 1;
	
	public LidlibCommon(Context context)
	{
		sContext = context;	
		
        handler = new Handler(){
        	public void handleMessage(Message msg){
        		switch(msg.what){
        		case HANDLER_SHOW_DIALOG:
        			showDialog(((DialogMessage)msg.obj).title, ((DialogMessage)msg.obj).message);
        			break;
        		}
        	}
        };		
	}
	
    public static void showMessageBox(String title, String message)
    {
    	Message msg = new Message();
    	msg.what = HANDLER_SHOW_DIALOG;
    	msg.obj = new DialogMessage(title, message);
    	
    	handler.sendMessage(msg);
    } 
	
    public static String getPackageName() {
    	return sPackageName;
    }    
    
    public void setPackageName(String packageName) 
    {
    	sPackageName = packageName;
    	
    	String apkFilePath = "";
        ApplicationInfo appInfo = null;
        PackageManager packMgmr = sContext.getPackageManager();
        try {
            appInfo = packMgmr.getApplicationInfo(packageName, 0);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException("Unable to locate assets, aborting...");
        }
        apkFilePath = appInfo.sourceDir;
        Log.w("apk path", apkFilePath);

        // add this link at the renderer class
        nativeSetPaths(apkFilePath);
    }
	
	public static String[] getAssetsFolderFiles(String localPath)
	{
		AssetManager assetManager = sContext.getAssets();
		ArrayList<String> resultCollection =  new ArrayList<String>();
		
		String[] tempList = null;
		try
		{
			tempList = assetManager.list(localPath);			
			String temp = new String();
			for (int i = 0; i < tempList.length; ++i)
			{		
				
				temp = localPath + "/" + tempList[i];
				try
				{
					InputStream iStream = assetManager.open(temp);
					iStream.close();   
					resultCollection.add(tempList[i]);
                }
                catch(IOException e)
                { 

                }
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}

		String[] resultArray = new String[resultCollection.size()];
		int i = 0;
		for (String str : resultCollection) {
			resultArray[i] = str;
			i++;
		}
	
		return resultArray;
	}
	
	public static String[] getAssetsFolderDirectories(String localPath)
	{
		AssetManager assetManager = sContext.getAssets();
		ArrayList<String> resultCollection =  new ArrayList<String>();
		
		String[] tempList = null;
		try
		{
			tempList = assetManager.list(localPath);			
			String temp = new String();
			for (int i = 0; i < tempList.length; ++i)
			{		
				
				temp = localPath + "/" + tempList[i];
				try
				{
					InputStream iStream = assetManager.open(temp);
					iStream.close();      
                }
                catch(IOException e)
                {
                   resultCollection.add(tempList[i]);
                }
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}

		String[] resultArray = new String[resultCollection.size()];
		int i = 0;
		for (String str : resultCollection) {
			resultArray[i] = str;
			i++;
		}
	
		return resultArray;
	}
	
	public static byte[] getAssetsFileData(String localPath)
	{
		AssetManager assetManager = sContext.getAssets();
		try
		{
			InputStream iStream = assetManager.open(localPath);
			byte[]  buf = new byte[ iStream.available() ];
			iStream.read( buf );
			iStream.close();      
			return buf;
        }
        catch(IOException e)
        {
        	e.printStackTrace();
        }
		
		return null;
	}
	
	public static boolean isAssetsFileExists(String localPath)
	{
		AssetManager assetManager = sContext.getAssets();
		try
		{
			InputStream iStream = assetManager.open(localPath);
			iStream.close();      
			return true;
        }
        catch(IOException e)
        {
        }
		
		return false;
	}
	
    private void showDialog(String title, String message)
    {
    	Dialog dialog = new AlertDialog.Builder(sContext)
	    .setTitle(title)
	    .setMessage(message)
	    .setPositiveButton("Ok",
	    new DialogInterface.OnClickListener()
	    {
	    	public void onClick(DialogInterface dialog, int whichButton){
	    		
	    	}
	    }).create();

	    dialog.show();
    }
}
